package com.java.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.java.dao.CompanyDao;
import com.java.model.Company;
import com.java.model.PageBean;
import com.java.util.DateUtil;
import com.java.util.StringUtil;

/**
 * department
 * 
 * @author ld
 *
 */
@Repository("companyDao")
public class CompanyDaoImpl implements CompanyDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Company> find(PageBean pageBean, Company s_company) {
		StringBuffer sb = new StringBuffer("select * from t_company");
		if (s_company != null) {
			if (null != s_company.getName()) {
				sb.append(" and name like '%" + s_company.getName() + "%'");
			}
		}
		if (pageBean != null) {
			sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
		}
		final List<Company> companyList = new ArrayList<Company>();
		System.out.println(sb.toString().replaceFirst("and", "where"));
		jdbcTemplate.query(sb.toString().replaceFirst("and", "where"), new Object[] {}, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Company company = new Company();
				company.setId(rs.getInt("id"));
				company.setName(rs.getString("name"));

				company.setAddress(rs.getString("address"));
				company.setPhone(rs.getString("phone"));
				company.setRemark(rs.getString("remark"));
				companyList.add(company);
			}
		});
		return companyList;
	}

	@Override
	public int count(Company s_company) {
		StringBuffer sb = new StringBuffer("select count(*) from t_company");
		if (s_company != null) {
			if (StringUtil.isNotEmpty(s_company.getName())) {
				sb.append(" and name like '%" + s_company.getName() + "%'");
			}
		}
		return jdbcTemplate.queryForObject(sb.toString().replaceFirst("and", "where"), Integer.class);
	}

	@Override
	public void add(Company company) {
		String sql = "insert into t_company values(null,?,?,?,?)";
		jdbcTemplate.update(sql,
				new Object[] { company.getName(), company.getAddress(), company.getPhone(), company.getRemark() });
	}

	@Override
	public Company loadById(int id) {
		String sql = "select * from t_company where id=?";
		final Company company = new Company();
		jdbcTemplate.query(sql, new Object[] { id }, new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				company.setId(rs.getInt("id"));
				company.setName(rs.getString("name"));
				company.setAddress(rs.getString("address"));
				company.setPhone(rs.getString("phone"));
				company.setRemark(rs.getString("remark"));
			}
		});
		return company;
	}

	@Override
	public void update(Company company) {
		String sql = "update t_company set name=?,address=?,phone=?,remark=? where id=?";
		jdbcTemplate.update(sql, new Object[] { company.getName(), company.getAddress(), company.getPhone(),
				company.getRemark(), company.getId() });
	}

	@Override
	public void delete(int id) {
		String sql = "delete from t_company where id=?";
		jdbcTemplate.update(sql, new Object[] { id });
	}

}
